# Tool che crea diversi tipi di archivi dati i files da includere
# by Michele 'MaZzo' Mazzoni 2007 GPL 2

import sys, os, datetime, time

# Configuration
# Distribution Info
name = 'deckbuilder'
version = '0.1.1'
# Files to include
files = 'src/Languages/* src/Skins/Default/* src/cards.db src/settings.xml src/*.py'


# Arguments
dobzip2 = 0
dogzip = 0
dorar = 0
dozip = 0

for arg in sys.argv:
    if arg == '--help':
       print('Usage: python dist.py [OPTION...]')
       print('Tool che crea diversi tipi di archivi dati i files da includere.')
       print('')
       print('Examples:')
       print("   python dist.py -bz -gz -rr -zp -name=foo -files='bar' -ver=1.2.3")
       print("   python dist.py -name=abc -files='de fg/*' -ver=1.2.3")
       print('')
       print('Options:')
       print('   -bz                           Create the bzip2 archive')
       print('   -gz                           Create the gzip archive')
       print('   -rr                           Create the rar archive')
       print('   -zp                           Create the zip archive')
       print('   -name=NAME                    Name of the dist')
       print('   -ver=VERSION                  Version of the dist')
       print('   -files=FILES                  Files to be included')
       print('')
       sys.exit()
    elif arg.startswith('-name='):
        name = arg[6:]
    elif arg.startswith('-ver='):
        version = arg[5:]
    elif arg.startswith('-files='):
        files = arg[7:]
    elif arg.startswith('-bz'):
        dobzip2 = 1
    elif arg.startswith('-gz'):
        dogzip = 1
    elif arg.startswith('-rr'):
        dorar = 1
    elif arg.startswith('-zp'):
        dozip = 1

if dorar == 0 and dogzip == 0 and dobzip2 == 0 and dozip == 0:
    dorar = 1
    dogzip = 1
    dobzip2 = 1
    dozip = 1

# Version
build = str(time.time())[5:9]
versionbuild = '%s.%s' % (version, build)

# Get the timestamp
dt = datetime.datetime.fromtimestamp(time.time())
timestamp = '%s-%s-%s' % (dt.day ,dt.month ,dt.year)

# Set the dist name and path
distname = '%s-%s-%s' % (name,versionbuild,timestamp)
distpath = 'dist/%s' % distname

# Create the rar archive
if dorar:
    rar = 'rar a -inul %s.rar %s' % (distpath,files)
    os.system(rar)
# Create the gzip archive
if dogzip:
    gzip = 'tar -czf %s.tar.gz %s' % (distpath,files)
    os.system(gzip)
# Create the bzip2 archive
if dobzip2:
    bzip2 = 'tar -cjf %s.tar.bz2 %s' % (distpath,files)
    os.system(bzip2)
# Create the zip archive
if dozip:
    zip = 'zip -q %s.zip %s' % (distpath,files)
    os.system(zip)